import 'dart:io';

import 'package:dio/dio.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';

import 'net_manager.dart';

/// 请求header拦截器
/// 主要是为每个请求添加header
class HttpHeaderInterceptor extends InterceptorsWrapper {
  HttpHeaderInterceptor();

  @override
  onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    var token = await netManager.getToken();
    // Address.token = token;
    if (token?.isNotEmpty == true) {
      options.headers["Authorization"] = token;
    }
    // l.d('http_log', 'token:$token');
    if (options.method == "GET") {
      //options.headers["Content-Type"] = "application/x-www-form-urlencoded";

      ///get方法要把header中的data全部置为null,才不会在遇到强校验的api域名的时候出现403错误
      options.data = null;
    } else if (options.method == "POST") {
      options.headers["Content-Type"] = "application/json;charset=UTF-8";
    }
    options.headers["User-Agent"] = await netManager.userAgent();
    // options.headers["Accept"] = "application/json;charset=UTF-8";
    // options.headers["app_version"] = version.versionLocal;
    options.headers["api_version"] = "1.0.0";
    options.headers["device"] = Platform.operatingSystem;
    handler.next(options);
  }
}
